

let position;
let distance;
let toAngle;
let to3D;
let initialpitch;//相机旋转前的角度



const cameraRotation = {
  init(_position,_distance,_toAngle,_to3D){
    viewer.clock.onTick.removeEventListener(cameraRotation.exection);
    position = _position;
    distance = _distance;
    toAngle = _toAngle;
    to3D = _to3D;
    setTimeout(cameraRotation.rotation,100);
    ;
  },

  rotation(){
    let startTime = Cesium.JulianDate.fromDate(new Date()); //创建一个新实例
    let stopTime = Cesium.JulianDate.addSeconds(startTime, 2, new Cesium.JulianDate()); //2秒后停止
    viewer.clock.startTime = startTime.clone();
    viewer.clock.stopTime = stopTime.clone();
    viewer.clock.currentTime = startTime.clone(); // 当前时间
    viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; // 行为方式
    viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK; // 时钟设置为当前系统时间; 忽略所有其他设置。
    initialpitch = viewer.camera.pitch;
    viewer.clock.onTick.addEventListener(cameraRotation.exection);
  },

  exection(){
    if (Cesium.JulianDate.compare(viewer.clock.currentTime, viewer.clock.stopTime) >= 0) {
      viewer.clock.onTick.removeEventListener(cameraRotation.exection);
    }

    let angle = to3D?30:-30; //值越大，旋转越快

    let delTime = Cesium.JulianDate.secondsDifference(viewer.clock.currentTime, viewer.clock.startTime);
    let pitch = Cesium.Math.toRadians(delTime * angle) + initialpitch;
    viewer.scene.camera.setView({
      destination: position, // 点的坐标
      orientation: {
        heading: Cesium.Math.toRadians(360),
        pitch: pitch,
      }
    });
    viewer.scene.camera.moveBackward(distance);
    cameraRotation.removeMyEvent();
  },
  removeMyEvent(){
    if (to3D && Cesium.Math.toDegrees(viewer.camera.pitch) >= toAngle) { //目标-55，旋转之前-90
      console.log("remove1");
      viewer.clock.onTick.removeEventListener(this.exection);
    }
    else if(!to3D && Cesium.Math.toDegrees(viewer.camera.pitch) <= toAngle){ //目标-90，旋转之前任意
      console.log("remove2");


      viewer.clock.onTick.removeEventListener(this.exection);
      viewer.scene.morphTo2D(0.0)
      setTimeout(viewer.scene.screenSpaceCameraController.enableZoom = true,500);
    }
  }





};
export default cameraRotation
